/**
 * @file UserKvSave.c
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2023-07-13
 * 
 * @copyright Copyright (c) 2023  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-07-13     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#include "stdio.h"
#include "crc16.h"
#include "UserKvSave.h"
#include "rtc.h"
//-------------------------------------------------------------
void UserFlashSaveInit(void)
{
#if(FLASH_CONTOR_TYPE ==  USE_FLASHDB)
#ifdef FAL_USING_SFUD_PORT
    extern int spi_flash_init(void);
    spi_flash_init();
#endif
    FlashDdInit();
#elif((FLASH_CONTOR_TYPE ==  USE_EASYFLASH))
    easyflash_init();
#endif
}

size_t user_get_env_blob(const char *key, void *value_buf, size_t buf_len)
{
#if(FLASH_CONTOR_TYPE ==  USE_FLASHDB)
    struct fdb_blob blob;
    return fdb_kv_get_blob(&xKvdb, key, fdb_blob_make(&blob, value_buf, buf_len));
#elif((FLASH_CONTOR_TYPE ==  USE_EASYFLASH))
    uint32_t saveLen = 0;
    return ef_get_env_blob(key,value_buf,buf_len,&saveLen);
    // return 0;
#endif
}
size_t user_set_env_blob(const char *key, void *value_buf, size_t buf_len)
{
#if(FLASH_CONTOR_TYPE ==  USE_FLASHDB)
    struct fdb_blob blob;
    return fdb_kv_set_blob(&xKvdb, key, fdb_blob_make(&blob, value_buf, buf_len));
#elif((FLASH_CONTOR_TYPE ==  USE_EASYFLASH))
    return ef_set_env_blob(key,value_buf,buf_len);
#endif
}

void UserHalRtcInit(void)
{
    uint32_t rtcValue;
    UTCTimeStruct setTime;
    if(user_get_env_blob(UTC_TIME_KEY,&rtcValue,sizeof(rtcValue)))
    {
        osal_ConvertUTCTime(&setTime, rtcValue);
        HalRtcInit(&setTime);
        LOG("Save utc = 0x%X\r\n", rtcValue);
    }else{
        HalRtcInit(NULL);
    }
}


